package alarm.ruiqi.com.alarm_pro.domain;


import android.text.TextUtils;
import android.text.format.DateUtils;

import com.lidroid.xutils.db.annotation.Column;
import com.lidroid.xutils.db.annotation.Id;
import com.lidroid.xutils.db.annotation.Table;
import com.lidroid.xutils.db.annotation.Transient;
import com.lidroid.xutils.util.LogUtils;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;

/**
 * Created by 徐宇健 on 2015/6/29 0029.
 * 闹钟实体
 */
@Table(name = "alarm")
public class AlarmEntity implements Serializable {

    @Id
    @Column(column = "id")
    int id ;
    @Column(column = "name")
    String name;
    @Column(column = "alarmTime")
    Long alarmTime;
    @Column(column = "description")
    String description;

    @Column(column = "repeat")
    String repeat_s;        //sqlite存储重复周期的数据

  /*  @Transient
    Set<Integer> repeat = new HashSet<Integer>();//重复周期*/

    @Column(column = "alarmUri")
    String alarmUri = "";//闹钟uri

    @Column(column = "alarmUriTitle")
    String alarmUriTitle;//闹钟uri标题

    @Column(column = "enable")
    boolean enable;//是否可用

//    @Column(name = "state")
//    int state = enable?1:0; //1 >> 可用; 0 >> 禁用 ; -1 >> 已过期
//
//    public int getState() {
//        return state;
//    }
//
//    public void setState(int state) {
//        this.state = state;
//    }

    @Transient
    private Calendar calendar;

    private int interValDate = 0;//距离下一个闹钟的天数 默认为0


    public Calendar getCalendar() {
        if (calendar == null){
            calendar = Calendar.getInstance();
            calendar.setTimeInMillis(alarmTime);

        }
        return calendar;
    }

    public void setCalendar(Calendar calendar) {
        this.calendar = calendar;
    }

    /**
     * 获取星期间隔
     * @param reCal 是否重新计算
     * @return
     */
    public int getInterValDate(boolean reCal) {
        if (reCal){
            return  getMinInterval(getHashSetRepeats(), Calendar.getInstance().get(Calendar.DAY_OF_WEEK),getAlarmTime() < System.currentTimeMillis());
        }else {
            return interValDate;
        }
    }

    public void setInterValDate(int interValDate) {
        this.interValDate = interValDate;
    }

    public AlarmEntity() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getAlarmTime() {
        return alarmTime;
    }

    public void setAlarmTime(Long alarmTime) {
        this.alarmTime = alarmTime;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

/*    public Set<Integer> getRepeat() {
        return repeat;
    }

    public void setRepeat(Set<Integer> repeat) {
        this.repeat = repeat;
    }*/

    public String getRepeat_s() {
        return repeat_s;
    }

    public void setRepeat_s(String repeat_s) {
        this.repeat_s = repeat_s;
    }

    public String getAlarmUri() {
        return alarmUri;
    }

    public void setAlarmUri(String alarmUri) {
        this.alarmUri = alarmUri;
    }

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public String getAlarmUriTitle() {
        return alarmUriTitle;
    }

    public void setAlarmUriTitle(String alarmUriTitle) {
        this.alarmUriTitle = alarmUriTitle;
    }

    @Override
    public String toString() {
        return "AlarmEntity{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", alarmTime=" + SimpleDateFormat.getInstance().format(alarmTime) +
                ", description='" + description + '\'' +
                ", repeat_s='" + repeat_s + '\'' +
                ", alarmUri='" + alarmUri + '\'' +
                ", alarmUriTitle='" + alarmUriTitle + '\'' +
                ", enable=" + enable +
               /* ", calendar=" + calendar +*/
                ", interValDate=" + interValDate +
                '}';
    }

    public HashSet<Integer> getHashSetRepeats(){
        HashSet<Integer> rp = new HashSet<>();
        if (TextUtils.isEmpty(repeat_s)) return rp;
        String [] ss =  repeat_s.split(",");
        for (String s:ss){
            if (!TextUtils.isEmpty(s) || "null".equals(s.trim())){
                try {
                    rp.add(Integer.parseInt(s));
                }catch (Exception e){
                    LogUtils.i(s);
                }
            }
        }
        rp.remove(null);
        return  rp;
    }

    public String setRepeat_s(HashSet<Integer> set){
        StringBuffer sb = new StringBuffer();
        set.remove(null);//清除默认存在的空数据
        for (Integer i:set){
            sb.append(i);
            sb.append(",");
        }
        repeat_s  = sb.toString();
        return  repeat_s;
    }

    public String getAlarminfo() {
        return DateUtils.getRelativeTimeSpanString(getAlarmTime(), System.currentTimeMillis(),
                DateUtils.SECOND_IN_MILLIS).toString() + "响铃";
    }

    //获取与指定日期的最短间隔
    @Deprecated
    public int getMinInterval(HashSet<Integer> repeat , int dayOfWeek){
        interValDate = 7;
        if (repeat.size() ==0) {return interValDate;   }; //一周

        for (Integer i:repeat){
            int temp  = getInerval(i,dayOfWeek);
            interValDate = temp <interValDate? temp:interValDate;
        }
        return interValDate;
    }

    /**
     * flag指同一天时  alarmTimeMinNow < 当前时间
     * @param repeat
     * @param dayOfWeek
     * @param alarmTimeMinNow
     * @return
     */
    public int getMinInterval(HashSet<Integer> repeat , int dayOfWeek, boolean alarmTimeMinNow){
        interValDate = 7;
        if (repeat.size() ==0) {return interValDate;   }; //一周
        if (repeat.size() >=7) {
            interValDate = 0;
            return interValDate;    //每天
        }
        for (Integer i:repeat){
            int temp  = getInerval(i,dayOfWeek);
            if ( temp == 0){
                temp =  alarmTimeMinNow ? 7 : 0;
            }
            interValDate = temp <interValDate? temp:interValDate;
        }
        return interValDate;
    }

    private int getInerval(int dayOfWeek, int currentDay) {
        return currentDay<=dayOfWeek? dayOfWeek-currentDay:7-currentDay+dayOfWeek;
    }


    public  String getAlarm_Format(){
        return SimpleDateFormat.getInstance().format(new Date(getAlarmTime()));
    }
}
